<template>
  <div class="paying">
    <!-- 1.地址 -->
    <div class="addressBox">
      <p class="addressTitle">
        收货地址 <i class="iconfont icon-xiangxia" @click="showAddr = !showAddr">&nbsp</i>
      </p>
      <div class="addressList"  v-show="showAddr">
        <ul>
          <li :class="addrList.isSelect?'item':''" v-for="(addrList, index) in addrList" :key="index" @click="changeStyle(index)"><p>{{addrList.uname}} {{addrList.address}} {{addrList.phone}}</p></li>
        </ul>
      </div>
      <div class="addressShow">
        <div>
          默认收货人：<span>{{selectAddr.uname}}</span>详细地址：<span>{{selectAddr.address}}</span>电话号码：<span
            >{{selectAddr.phone}}</span
          >
        </div>
      </div>
    </div>
    <!-- 2.商品 -->
    <div class="payGoods">
      <div class="payGoodsTitle">商品清单</div>
      <div class="cartItem" v-for="(goodsList, index) in payList" :key="index">
        <div class="goodsImg"><img :src="goodsList.imgurl" /></div>
        <div class="goodsTitle">{{goodsList.title}}</div>
        <div class="goodsParams">
          <div>{{goodsList.color}}</div>
          <div>{{goodsList.size}}</div>
        </div>
        <div class="goodsNum">x{{goodsList.num}}</div>
      </div>
    </div>
    <!-- 3.支付 -->
    <div class="pay">
      <div class="payWay">
        <div class="payWayTitle">请选择支付方式</div>
        <div class="payWayImg">
          <img class="payWayBorder" src="./images/微信支付.png" /><img
            src="./images/支付宝支付.png"
          />
        </div>
      </div>
      <div class="payDetail">
        <div class="item goodsNum">商品数量：<span>{{totalCount}}件</span></div>
        <div class="item goodsCount">折扣：<span>0.00</span></div>
        <div class="item suilv">税率：<span>0.00%</span></div>
        <div class="item total">总价：<span>{{totalPrice}}.00</span></div>
        <el-button type="text" class="payButton" @click="open">付款</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
export default {
  data(){
    return{
      showAddr:false,
      selectIndex:0
    }
  },
  mounted(){
    this.$store.dispatch('getAddress')
  },
  computed:{
  payList(){
    return this.$route.params.payList
  },
  totalCount(){
    return this.payList.reduce((pre,next)=>pre+next.num,0)
  },
  totalPrice(){
    return this.payList.reduce((pre,next)=>pre+next.price*next.num,0)
  },
  ...mapState({ addrList:state=>state.paying.addressList || []}),
  selectAddr(){
    return this.addrList[this.selectIndex]
  },
  },
  methods: {
      open() {
        this.$alert(`<img src="./images/二维码.png">`, '扫码支付', {
          confirmButtonText: '查看订单',
          cancelButtonText: '返回首页',
          showCancelButton: true,
          center:true,
          dangerouslyUseHTMLString: true,
          beforeClose:(action, instance, done)=>{ 
           if(action=='confirm'){
              //关闭盒子
              done();
              //路由跳转
              this.$router.push('/center');
           }else if(action=='cancel'){
              //关闭盒子
              done();
              this.$router.push('/home');
           }
        }
        });
      },
      //改变地址选择样式
      changeStyle(index){
        this.addrList.forEach(item=>{
          item.isSelect = false
        })
        this.addrList[index].isSelect = true
        this.selectIndex = index
      }
    }
}
</script>

<style lang="less" scoped>
.paying {
  width: 80%;
  margin: 0 auto;
}
.addressBox {
  padding-top: 110px;
  margin-bottom: 30px;
  border-bottom: 1px solid #d6d6d6;
  .addressTitle {
    font-size: 1.5em;
  }
  .addressList {
    width: 48%;
    margin: 10px 50px;
    border: 1px solid #333;
    border-radius: 10px;
    line-height: 36px;
    li {
      margin: 10px 0px;
    }
    .item {
      background-color: rgb(225, 220, 220);
    }
  }
  .addressShow {
    margin-top: 15px;
    margin-bottom: 30px;
    font-size: 1.4em;
    span {
      color: #545454;
      margin-right: 26px;
    }
  }
}
.payGoods {
  margin-top: 30px;
  padding-bottom: 30px;
  border-bottom: 1px solid #d6d6d6;
  .payGoodsTitle {
    text-align: center;
    font-size: 1.5em;
    line-height: 40px;
    background-color: rgb(198, 198, 198);
  }
  .cartItem {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-top: 10px;
    .goodsImg {
      width: 20%;
      img {
        width: 90%;
      }
    }
    .goodsTitle {
      width: 50%;
    }
    .goodsParams {
      width: 20%;
    }
    .goodsNum {
      width: 40%;
    }
  }
}
.payWay {
  display: flex;
  flex-direction: row;
  align-items: center;
  margin-top: 30px;
  .payWayTitle {
    font-size: 1.5em;
    line-height: 40px;
  }
  .payWayImg {
    background-color: #fff;
    img {
      padding: 20px;
      margin: 6px 40px;
    }
    .payWayBorder {
      border: 1px solid #333;
    }
  }
}
.payDetail {
  margin-top: 15px;
  .item{
    margin-bottom: 5px;
  }
  .total{
    font-size: 1.5em;
    color: red;
  }
  .payButton{
  width: 20%;
  margin-top: 15px;
  margin-bottom: 30px;
  border: none;
  border-radius: 0;
  font-size: 16px;
  line-height: 15px;
  color:#fff;
  background-color: #333;
 }
  
}
</style>
